// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.query.sections;

import java.util.Set;

import org.dengues.designer.ui.i18n.Messages;
import org.dengues.designer.ui.query.AbstractFormPage;
import org.dengues.model.database.SectionColumns;
import org.dengues.ui.query.ComputedColumnDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-12-8 qiang.zhang $
 * 
 */
public class ColumnsSection extends TableSectionPart {

    /**
     * Qiang.Zhang.Adolf@gmail.com ColumnsSection constructor comment.
     * 
     * @param parent
     * @param toolkit
     * @param style
     */
    public ColumnsSection(AbstractFormPage page, Composite parent) {
        super(
                page,
                parent,
                new String[] {
                        Messages.getString("ColumnsSection.add"), Messages.getString("ColumnsSection.edit"), Messages.getString("ColumnsSection.remove"), Messages.getString("ColumnsSection.up"), Messages.getString("ColumnsSection.down") }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
        getSection().setText(Messages.getString("ColumnsAliasFormPage.text")); //$NON-NLS-1$
        getSection().setDescription(Messages.getString("ColumnsSection.description")); //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.query.sections.TableSectionPart#buttonSelected(org.eclipse.swt.widgets.Button, int)
     */
    @Override
    protected void buttonSelected(Button button, int index) {
        handleButtonSelected(index, TYPE_COLUMNS);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.query.sections.AbstractSectionPart#createMainTitle(org.eclipse.swt.widgets.Composite,
     * int, org.eclipse.ui.forms.widgets.FormToolkit)
     */
    @Override
    protected void createMainTitle(Composite parent, int span, FormToolkit toolkit) {
        final Button button = new Button(parent, SWT.CHECK);
        button.setText(Messages.getString("ColumnsSection.disc")); //$NON-NLS-1$
        button.setSelection(dbDiagram.isDistinct());
        new Label(parent, SWT.NONE);
        button.addSelectionListener(new SelectionAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                dbDiagram.setDistinct(button.getSelection());
                markDirty();
            }
        });
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.query.sections.TableSectionPart#handleDoubleClick(org.eclipse.jface.viewers.IStructuredSelection)
     */
    @Override
    protected void handleDoubleClick(IStructuredSelection selection) {
        Set<String> existNames = getExistNames(TYPE_COLUMNS).keySet();
        Object o = selection.getFirstElement();
        if (o instanceof SectionColumns) {
            SectionColumns columns = (SectionColumns) o;
            existNames.remove(columns.getName());
            ComputedColumnDialog dialog = new ComputedColumnDialog(Display.getDefault().getActiveShell(), dbDiagram, columns
                    .getName(), columns.getExpression(), existNames);
            if (ComputedColumnDialog.OK == dialog.open()) {
                columns.setName(dialog.getName());
                columns.setExpression(dialog.getExpression());
                viewerPart.refresh();
                markDirty();
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.query.sections.TableSectionPart#createTableColumns()
     */
    @Override
    public void createTableColumns(TableViewer viewerPart) {
        Table table = viewerPart.getTable();
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText(Messages.getString("ColumnsSection.colName")); //$NON-NLS-1$
        column.pack();
        column = new TableColumn(table, SWT.NONE);
        column.setText(Messages.getString("ColumnsSection.expression")); //$NON-NLS-1$
        column.pack();
        ViewerPartProvider provider = new ViewerPartProvider(TYPE_COLUMNS);
        viewerPart.setContentProvider(provider);
        viewerPart.setLabelProvider(provider);
        viewerPart.setInput(dbDiagram);

    }
}
